<?php

namespace App\Http\Controllers\Admin\V1;

use Illuminate\Http\JsonResponse;
use App\Extensions\Log\V1\InstanceLog;
use App\Services\Admin\V1\MenuService;
use App\Exceptions\Admin\V1\AdminException;
use App\Http\Resources\Admin\V1\MenuResource;
use App\Exceptions\Admin\V1\GeneralException;
use App\Http\Requests\Admin\V1\MenuStoreRequest;
use App\Http\Requests\Admin\V1\MenuUpdateRequest;
use App\Http\Requests\Admin\V1\MenuDestroyRequest;
use Illuminate\Database\Eloquent\ModelNotFoundException;

// 控制器编号05
class MenuController extends Controller
{
    /**
     * @var MenuService
     */
    protected $menu_service;

    /**
     * MenuController constructor.
     * @param MenuService $menuService
     */
    public function __construct(MenuService $menuService)
    {
        $this->menu_service = $menuService;
    }

    /**
     * @expalin 获取全部菜单
     * @return JsonResponse
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/11:05
     */
    public function index()
    {
        $menus = $this->menu_service->all();

        return $this->success(MenuResource::collection($menus));
    }

    /**
     * @expalin 创建菜单
     * @param MenuStoreRequest $menuStoreRequest
     * @return JsonResponse
     * @throws AdminException
     * @throws GeneralException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/11:25
     */
    public function store(MenuStoreRequest $menuStoreRequest)
    {
        $parentId = (int) $menuStoreRequest->input('parent_id');
        if ($parentId) {
            try {
                $this->menu_service->find($parentId);
            }
            catch ( ModelNotFoundException $exception ) {
                InstanceLog::instance('admin')->error('新增菜单失败：' . $exception->getMessage());

                throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
            }
        }

        try {
            $this->menu_service->store($menuStoreRequest);
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('新增菜单失败：' . $exception->getMessage());

            throw new AdminException(AdminException::MENU_MENU_CREATE_FAULT);
        }

        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.menu.success_menu_store'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 更新菜单
     * @param MenuUpdateRequest $menuUpdateRequest
     * @return JsonResponse
     * @throws AdminException
     * @throws GeneralException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/11:35
     */
    public function update(MenuUpdateRequest $menuUpdateRequest)
    {
        $parentId = (int) $menuUpdateRequest->input('parent_id');
        if ($parentId) {
            try {
                $this->menu_service->find($parentId);
            }
            catch ( ModelNotFoundException $exception ) {
                InstanceLog::instance('admin')->error('编辑菜单失败：' . $exception->getMessage());

                throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
            }
        }

        try {
            $this->menu_service->update($menuUpdateRequest->input('id'), $menuUpdateRequest);
        }
        catch ( ModelNotFoundException $exception ) {
            InstanceLog::instance('admin')->error('更新菜单失败：' . $exception->getMessage());

            throw new GeneralException(GeneralException::GENERAL_SYSTEM_RETRIEVE_UNKNOWN);
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('更新菜单失败：' . $exception->getMessage());

            throw new AdminException(AdminException::MENU_MENU_UPDATE_FAULT);
        }

        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.menu.success_menu_update'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 删除菜单
     * @param MenuDestroyRequest $menuDestroyRequest
     * @return JsonResponse
     * @throws AdminException
     * @throws GeneralException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/11:44
     */
    public function destroy(MenuDestroyRequest $menuDestroyRequest)
    {
        try {
            $menu = $this->menu_service->find($menuDestroyRequest->input('id'));
        }
        catch ( ModelNotFoundException $exception ) {
            InstanceLog::instance('admin')->error('删除菜单失败：' . $exception->getMessage());

            throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
        }

        try {
            $bool = $this->menu_service->destroy($menu);
        }
        catch ( \Throwable $throwable ) {
            InstanceLog::instance('admin')->error('删除菜单失败:' . $throwable->getMessage());

            throw new GeneralException(GeneralException::GENERAL_SYSTEM_RETRIEVE_UNKNOWN);
        }

        if (!$bool) {
            throw new AdminException(AdminException::EXISTS_ADMIN_MENUS_SUB);
        }

        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.menu.success_menu_delete'),
        ];

        return $this->success($result);
    }
}
